package com.test.controller;

import java.io.InputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.io.IOUtils;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import com.test.consts.Const;

@Controller
public class Hello {
    
    private static Logger logger = LogManager.getLogger(Hello.class.getName());
    
    
    @RequestMapping(value = "/hello", method = RequestMethod.POST)
    public String expectation(HttpServletRequest request, HttpServletResponse response, ModelMap model)
            throws InterruptedException {
        String requestBody = null;
        try {
            InputStream input = request.getInputStream();
            requestBody = IOUtils.toString(input, "utf-8");
            
            System.out.println("requestBody: "+requestBody);
            String successLog = "get ok!!";

            model.addAttribute("code", 200);
            model.addAttribute("msg", successLog);
        } catch (Exception e) {
            logger.error(
                    String.format("parse body error! raw_data:%s;ip=%s", requestBody, request.getHeader("X-From-Ip")),
                    e);
            throw new IllegalArgumentException("parse body error! raw_data:" + requestBody);
        }
        return Const.DEFAULT_VIEW;
        
    }
}
